//
//  EveryDaySendCollectionViewCell.m
//  NewMusic
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "EveryDaySendCollectionViewCell.h"
#import "EveryDaySendTableViewCell.h"
#import <MJRefresh.h>
@interface EveryDaySendCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger totalPage;
@end

@implementation EveryDaySendCollectionViewCell
- (void)dealloc {
    [_tableView release];
    [_dataArray release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = [NSMutableArray array];
        self.page = 1;
        //创建视图
        [self createSubView];
        //获取数据
        [self loadData];
        //加载数据
        [self loadNewData];
    }
    return self;
}
#pragma mark 刷新和加载数据
- (void)loadNewData {
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.page++;
        if (self.page > self.totalPage) {
            [self.tableView.footer noticeNoMoreData];
        }else {
            [self loadData];
        }
    }];
}
#pragma mark 获取数据
- (void)loadData {
    [CustomerNetworking customerConnectWithURL:[NSString stringWithFormat:@"http://api.dongting.com/channel/channel/mvs?page=%ld&size=5&app=ttpod&v=v8.1.1.2015110616", self.page] andParameters:nil andBlock:^(id response) {
        self.totalPage = [[response valueForKey:@"pageCount"] integerValue];
        NSArray *array = [response valueForKey:@"data"];
        if (array.count != 0) {
            for (NSDictionary *dic in array) {
                TheNewMVModel *model = [[TheNewMVModel alloc] initWithDictionary:dic];
                [self.dataArray addObject:model];
                [model release];
            }
        }
        [self.tableView.footer endRefreshing];
        [_tableView reloadData];
    }];
}
#pragma mark 创建视图
- (void)createSubView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    //注册cell的重用标识
    [_tableView registerClass:[EveryDaySendTableViewCell class] forCellReuseIdentifier:@"EveryDaySendTableViewCellIdentify"];
    [self.contentView addSubview:_tableView];
    [_tableView release];
}
#pragma mark cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EveryDaySendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EveryDaySendTableViewCellIdentify" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    if (_dataArray.count != 0 ) {
        cell.theNewMVModel = [_dataArray objectAtIndex:indexPath.row];
        if ([_dataArray[indexPath.row] tagArray].count <= 3) {
            cell.showTag = NO;
        }else {
            cell.showTag = YES;
        }
    }
    cell.showButton.tag = 2800 + indexPath.row;
    //播放的点击事件
    [cell.showButton addTarget:self action:@selector(showButtonSendAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
#pragma mark cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count ? _dataArray.count : 0 ;
}
#pragma mark cell 的大小
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.height / 2;
}
#pragma mark 播放的点击事件
- (void)showButtonSendAction: (UIButton *)button {
    if (_dataArray.count != 0) {
        [self.delegate showButtonClickAction:[[_dataArray objectAtIndex:button.tag - 2800] mvList]];
    }
}
@end
